import {chunk} from "lodash";

export default class Sort {
    public orders: Order[] = [];

    public toString() {
        return this.orders.map(order => order.toString()).join(",")
    }

    public static of(orders?: string) {
        const sort = new Sort();
        if (orders) {
            const items: (string[])[] = chunk(orders.split(','), 2);
            sort.orders = items.map(item => new Order(item[0], Direction[item[1] as keyof typeof Direction]));
        }
        return sort;
    }
}

export class Order {
    public property: string;
    public direction: Direction;

    constructor(property: string, direction: Direction) {
        this.property = property;
        this.direction = direction;
    }

    public toString() {
        return `${this.property},${this.direction}`;
    }
}

export enum Direction {
    asc = 'asc',
    desc = 'desc'
}